package user.view;

import user.model.SituModel;
import user.model.UserModel;
import user.ucm.UCDeleting;
import user.ucm.UCLogining;
import user.ucm.UserInformation;

public class LoginView extends javax.swing.JFrame {
    private UserModel user;

    /** Creates new form LoginView */
    public LoginView() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginName_TextField = new javax.swing.JTextField();
        loginpsw_TextField = new javax.swing.JTextField();
        loginname_Label = new javax.swing.JLabel();
        loginpsw_Label = new javax.swing.JLabel();
        exit_Button = new javax.swing.JButton();
        login_Button = new javax.swing.JButton();
        delete_Button = new javax.swing.JButton();
        regist_Button = new javax.swing.JButton();
        note_Label = new javax.swing.JLabel();
        login_MenuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        edit_Menu = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CP3120");

        loginName_TextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        loginpsw_TextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        loginname_Label.setText("User name:");

        loginpsw_Label.setText("Password:");

        exit_Button.setText("Exit");

        login_Button.setText("Login");
        login_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                login_ButtonActionPerformed(evt);
            }
        });

        delete_Button.setText("Delete");
        delete_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delete_ButtonActionPerformed(evt);
            }
        });

        regist_Button.setText("Regist");
        regist_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regist_ButtonActionPerformed(evt);
            }
        });

        note_Label.setFont(new java.awt.Font("宋体", 0, 14));

        fileMenu.setText("File");
        login_MenuBar.add(fileMenu);

        edit_Menu.setText("Edit");
        login_MenuBar.add(edit_Menu);

        setJMenuBar(login_MenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(note_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(loginname_Label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(loginpsw_Label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(login_Button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(loginName_TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                    .addComponent(loginpsw_TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(delete_Button)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(regist_Button, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(exit_Button))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(loginname_Label)
                    .addComponent(loginName_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginpsw_Label)
                    .addComponent(loginpsw_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(note_Label, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(exit_Button)
                        .addComponent(login_Button)
                        .addComponent(delete_Button)
                        .addComponent(regist_Button)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void login_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_login_ButtonActionPerformed
        int res = 0;
        UserInformation ln = new UserInformation(this.get_UserModel());
        if ((res = ln.run()) == 0) {
            UCLogining l = new UCLogining(this.get_UserModel(), this.get_SituModel());
            if ((res = l.run()) == 0) {
                this.note_Label.setText("Pass!");
                MainView mv = new MainView(this.get_UserModel());
                mv.setVisible(true);
                this.setVisible(false);
            } else if (res == 1) {
//                this.note_Label.setText("The username is unvaluable!");
                ManagerView mv = new ManagerView();
                mv.setVisible(true);
            } else if (res == 9){
                AdminView av = new AdminView();
                av.setVisible(true);
            } else{
                this.note_Label.setText("The password is fault!");
            }
        } else {
            switch (res) {
                case 1:
                    this.note_Label.setText("Please entry user name and password.");
                    break;
                case 2:
                    this.note_Label.setText("The length of user name is uncorrect.");
                    break;
                case 3:
                    this.note_Label.setText("The length of password is uncorrect.");
                    break;
                default:
                    break; //Do nothing
            }
        }
}//GEN-LAST:event_login_ButtonActionPerformed

    private void regist_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_regist_ButtonActionPerformed
        RegisterView rv = new RegisterView();
        rv.setVisible(true);
    }//GEN-LAST:event_regist_ButtonActionPerformed

    private void delete_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete_ButtonActionPerformed
        int res = 0;
        UserInformation ln = new UserInformation(this.get_UserModel());
        if ((res = ln.run()) == 0) {
            UCDeleting ucd = new UCDeleting(this.get_UserModel());
            ucd.run();
        }else {
            switch (res) {
                case 1:
                    this.note_Label.setText("Please entry user name and password.");
                    break;
                case 2:
                    this.note_Label.setText("The length of user name is uncorrect.");
                    break;
                case 3:
                    this.note_Label.setText("The length of password is uncorrect.");
                    break;
                default:
                    break; //Do nothing
            }
        }
    }//GEN-LAST:event_delete_ButtonActionPerformed

    public UserModel get_UserModel() {
        UserModel um = new UserModel();
        um.setName(this.loginName_TextField.getText());
        um.setPsw(this.loginpsw_TextField.getText());
        return um;
    }

    public SituModel get_SituModel() {
        SituModel sm = new SituModel();
        //sm.setSituation(this.situ_ComboBox.getSelectedIndex());
        return sm;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LoginView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton delete_Button;
    private javax.swing.JMenu edit_Menu;
    private javax.swing.JButton exit_Button;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JTextField loginName_TextField;
    private javax.swing.JButton login_Button;
    private javax.swing.JMenuBar login_MenuBar;
    private javax.swing.JLabel loginname_Label;
    private javax.swing.JLabel loginpsw_Label;
    private javax.swing.JTextField loginpsw_TextField;
    private javax.swing.JLabel note_Label;
    private javax.swing.JButton regist_Button;
    // End of variables declaration//GEN-END:variables
}
